/**
 * @file 小程序核心逻辑层
 *
 *
 */
(function (global) {
  let wx = {};
  let classMap = {}; //记录下当前页面 方便后面调用
  class Logic {
    init() {
      this.uniqId = 0;
      const firstPageUrl = window.appJSON.pages[0];
      this.navigateTo(firstPageUrl);
    }
    // 生成唯一识别页面的Id
    _generateUniqId() {
      console.log( this.uniqId," this.uniqId:")
      return 'id' + this.uniqId++
    }

    navigateTo(uri) {
      const PageClass = classMap[uri];
      console.log(uri,'::')
      const page = new PageClass(this._generateUniqId(), uri)
    }

  }
  // 写基类
  class pageBase {
    constructor(id, uri) {
      this.uri = uri;
      this.id = id;
      this.initData();
      this._render().then(() => {
        global._bridge.postMessage(this.id,{
          type: 'initSet',
          data: this.data
        })
      });
    }
    _render() {
      return global._bridge.createView(this.id).then(frame => {
        this.$el = frame;
      });
    }
    initData() {
      // 为啥做这个  得到新数据 处理之后指向一个新的data对象
      this.data = JSON.parse(JSON.stringify(this.data))
    }
    setData() {
      window._bridge.postMessage(this.id,{
        type: 'setData',
        name:'zlf-update'
      })
    }
  }

  const createPageClass = options => {
    // 继承
    class Page extends pageBase {
      constructor(...args) {
        super(...args)
      }
    }
    Object.assign(pageBase.prototype, options)
    return Page;
  }
  // 记一下
  const Page = (uri, options) => {
    classMap[uri] = createPageClass(options);
  }

  global.Logic = new Logic();
  global.Page = Page;

})(window)